/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 ManagedConnectionListenerTester.hpp. Created on: 18.09.2009 13:29:37
 */

#ifndef MANAGEDCONNECTIONLISTENERTESTER_HPP_
#define MANAGEDCONNECTIONLISTENERTESTER_HPP_

#include "cpputils/TestUtils.hpp"
#include "smartobjects/network/INetworkFactory.hpp"
#include "smartobjects/network/IManagedConnection.hpp"
#include "smartobjects/network/IManagedConnectionListener.hpp"
#include "smartobjects/network/IManagedStreamListener.hpp"

using namespace smartobjects;

class ManagedConnectionListenerTester : public IManagedConnectionListener, public IManagedStreamListener {
public:
	typedef boost::shared_ptr<ManagedConnectionListenerTester> SharedPtr;
	typedef std::list<ConnectionHandler*> Handlers;

public:
	ManagedConnectionListenerTester();
	virtual ~ManagedConnectionListenerTester();

	virtual void onConnected(IManagedConnection*) throw();
	virtual void onConnected(ConnectionHandler*) throw();

	virtual void onDisconnected(IManagedConnection*) throw();
	virtual void onDisconnected(ConnectionHandler*) throw() ;

	virtual void onConnectionFailure(IManagedConnection* poManagedConnection, const std::string& sErrorMessage) throw();
	virtual void onConnectionFailure(ConnectionHandler* poHandler, const std::string& sErrorMessage) throw();

	virtual void onReceived(ConnectionHandler* poHandler, ByteBuffer* poBuffer) throw();
	virtual void onReceiveError(ConnectionHandler* poHandler, const std::string& sErrorMessage) throw();
	virtual void onWriteError(ConnectionHandler* poHandler, const std::string& sErrorMessage) throw();


	void reset();
	bool isConnected();
	std::string getLastErrorMessage();
	ByteBuffer* getLastReceivedMessage();
	const unsigned getReceiveEventCounter();

	boost::recursive_mutex& getMonitor();
	boost::recursive_mutex& getMonitorPerHandler();
	void waitForAnyEvent(boost::recursive_mutex::scoped_lock& oLock, const std::string& sLocation="Unknown location");
	void waitForAnyEventPerHandler(boost::recursive_mutex::scoped_lock& oLock, const std::string& sLocation="Unknown location");

	Handlers& getLastHandlers();

protected:
	void notifyWaiter();
	void notifyWaiterPerHandler();
	void addLastHandler(ConnectionHandler *poHandler);

private:
	bool m_bConnected;
	std::string m_sLastErrorMessage;
	unsigned m_nReceiveEventCounter;

	boost::condition m_evAwait;
	boost::recursive_mutex m_mxMonitor;

	boost::condition m_evAwaitPerHandler;
	boost::recursive_mutex m_mxMonitorPerHandler;

	ByteBuffer* m_poReceiveBuffer;

	Handlers m_aHandlers;
};

#endif /* MANAGEDCONNECTIONLISTENERTESTER_HPP_ */
